﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BA2009.Entities;

namespace BA2009.Data.DataAccess
{
    public class BannerPositionRepository : RepositoryBase<BA2009.Entities.BannerPosition>, IBannerPositionRepository
    {

        private MapTablesRepository positionMapRepository = new MapTablesRepository("BannerPositionID", "BannerID", "BannerPositionMap");


         #region IRepository<BannerPosition> Members


         #endregion

         /// <summary>
         /// Return list BannerPositions from BannerPosition of list.
         /// </summary>
         /// <returns></returns>
         public override IQueryable<BannerPosition> FindAll()
         {
              var listBannerPosition = from b in db.BannerPositions
                                       select new BA2009.Entities.BannerPosition
                                       {
                                            BannerPositionID = b.BannerPositionID,
                                            BannerPositionName = b.BannerPositionName,
                                            PageID=b.PageID,
                                            Description = b.Description,
                                            Code = b.Code
                                       };
              return listBannerPosition;
         }

         /// <summary>
         /// Return BannerPosition by id
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
         public override BannerPosition GetById(int id)
         {
              var bannerPosition = from b in db.BannerPositions
                                   where (b.BannerPositionID == id)
                                   select new BA2009.Entities.BannerPosition
                                   {
                                        BannerPositionID = id,
                                        BannerPositionName = b.BannerPositionName,
                                        Description = b.Description,
                                        Code = b.Code
                                   };

              return bannerPosition.SingleOrDefault();
         }

         /// <summary>
         /// Save changes
         /// </summary>
         /// <param name="linqEnity"></param>
         /// <param name="dbContext"></param>
         protected override void Save( object linqEnity, BA2009.DataAccess.Context.BA2009Context dbContext, params object [] maps )
         {
        

              var bannerPosition = linqEnity as BA2009.DataAccess.Entities.BannerPosition;

              dbContext.BannerPositions.InsertOnSubmit(bannerPosition);
              dbContext.SubmitChanges();

              if (maps.Length != 1)
              {
                  throw new InvalidOperationException("Tham số truyền vào hàm Save của BannerPositionRepository sai.");
              }

              IList<Entities.MapEntity> bannerPositionMaps = maps[0] as IList<Entities.MapEntity>;
              if (bannerPositionMaps != null)
              {
                  foreach (var positon in bannerPositionMaps)
                  {
                      positon.ID1 = bannerPosition.BannerPositionID;
                  }
                  positionMapRepository.Insert(bannerPositionMaps, dbContext);
              }
              dbContext.SubmitChanges();

         }

         /// <summary>
         /// Delete BannerPosition item from list.
         /// </summary>
         /// <param name="id"></param>
         /// <param name="dbContext"></param>
         protected override void Delete(int id, BA2009.DataAccess.Context.BA2009Context dbContext)
         {
             // Delete all map table
             var positionMap = (from pm in dbContext.BannerPositionMaps
                                where pm.BannerPositionID == id
                                select pm).AsEnumerable();

             dbContext.BannerPositionMaps.DeleteAllOnSubmit< BA2009.DataAccess.Entities.BannerPositionMap>(positionMap);

             dbContext.SubmitChanges();

             var bannerPosition = (from b in dbContext.BannerPositions
                                   where (b.BannerPositionID == id)
                                   select b).SingleOrDefault();
             dbContext.BannerPositions.DeleteOnSubmit(bannerPosition);
             dbContext.SubmitChanges();
         }

         /// <summary>
         /// Update Items
         /// </summary>
         /// <param name="itemToUpdate"></param>
         /// <param name="orginialItem"></param>
         /// <param name="dbContext"></param>
         protected override void Update( object itemToUpdate, object orginialItem, BA2009.DataAccess.Context.BA2009Context dbContext, params object [] maps )
         {

         
           

              var entity = itemToUpdate as BA2009.DataAccess.Entities.BannerPosition;
              var orginial = orginialItem as BA2009.DataAccess.Entities.BannerPosition;
              dbContext.BannerPositions.Attach(entity, orginial);
              dbContext.SubmitChanges();

              IList<Entities.MapEntity> positionMaps = maps[0] as IList<Entities.MapEntity>;
              if (positionMaps != null)
              {
                  positionMapRepository.Update(positionMaps, dbContext);
              }
              dbContext.SubmitChanges();
         }

         protected override object GetLinqEntity(int id)
         {
              return (from b in db.BannerPositions
                      where (b.BannerPositionID == id)
                      select b).SingleOrDefault();
         }



         #region IBannerPositionRepository Members

         public IQueryable <BannerPosition> GetByPageId(int pageId)
         {
              return  from b in db.BannerPositions
                     where (b.PageID == pageId)
                     select new BA2009.Entities.BannerPosition
                     {
                         BannerPositionID= b.BannerPositionID,
                         BannerPositionName=b.BannerPositionName,
                         Code=b.Code,
                         Description=b.Description,
                         PageID=pageId
                     };
         }

         #endregion

         #region IBannerPositionRepository Members

         IQueryable<BA2009.Entities.BannerPosition> IBannerPositionRepository.GetByPageId(int pageId)
         {
             throw new NotImplementedException();
         }

         #endregion
    }
}
